package edu.yctc.genesis.controller.impl;

import edu.yctc.genesis.constant.StaticPath;
import edu.yctc.genesis.constant.TemplatePath;
import edu.yctc.genesis.controller.UserController;
import edu.yctc.genesis.entity.ResultDO;
import edu.yctc.genesis.entity.UserDO;
import edu.yctc.genesis.service.UserIService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@ComponentScan({ "edu.yctc.genesis.service" })
@RequestMapping("/user")
public class UserControllerImpl implements UserController {

    @Autowired
    private UserIService userService;

    @Override
    @PostMapping("signup")
    public String signup(@ModelAttribute UserDO userDO) {
        ResultDO<Void> resultDO = userService.signup(userDO);
        if (resultDO.isSuccess() == false) {
            // TODO log
            return "redirect:/common/errorpage";
        }
        return "redirect:/" + StaticPath.USER_SIGNUP_SUCCESS;
    }

    @Override
    @GetMapping("signup")
    public String showSignup(ModelMap map) {
        map.addAttribute("userDO", new UserDO());
        return TemplatePath.USER_SIGNUP;
    }
}
